package com.bawei.demo_shoppingcar.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bawei.demo_shoppingcar.R;
import com.bawei.demo_shoppingcar.application.ContentData;
import com.bawei.demo_shoppingcar.bean.ClassifyBean;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;


/**
 * Created by HP on 2017/3/23.
 */

public class ClassifyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private final Context context;
    private ClassifyBean.DataBean data;
    private final int ITEM_PROPERTY = 0;
    private final int ITEM_SKIN = 1;
    private final int ITEM_EFFECT = 2;
    private MyClassDefaultItemClickListener myClassDefaultItemClickListener;
    private MyClassTabLayoutClickListener myClassTabLayoutClickListener;

    public ClassifyRecyclerAdapter(Context context, ClassifyBean.DataBean data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getItemViewType(int position) {

        if (position == 0) {
            return ITEM_PROPERTY;
        } else if (position == 1) {
            return ITEM_SKIN;
        }
        return ITEM_EFFECT;
    }



    /**
     * 创建default商品的回调接口
     */
    public interface MyClassDefaultItemClickListener {
        void onItemClick(View view, int position);
    }

    //在activity里面adapter就是调用的这个方法,将点击事件监听传递过来,并赋值给全局的监听
    public void setDefaultItemClickListener(MyClassDefaultItemClickListener myItemClickListener) {
        myClassDefaultItemClickListener = myItemClickListener;
    }
    //创建TabLayout的回调接口
    public interface MyClassTabLayoutClickListener {
        void onItemClick(View view, int position);
    }
    public void setTabLayoutClickListener(MyClassTabLayoutClickListener myItemClickListener) {
        myClassTabLayoutClickListener = myItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {

        View view = null;
        RecyclerView.ViewHolder viewHolder = null;

        switch (i) {
            case ITEM_PROPERTY:
                view = LayoutInflater.from(context).inflate(R.layout.classify_item_property, viewGroup, false);
                viewHolder = new Property(view);
                break;
            case ITEM_SKIN://功效
                view = LayoutInflater.from(context).inflate(R.layout.classify_item_skin, viewGroup, false);
                viewHolder = new Skin(view,myClassTabLayoutClickListener);
                break;
            case ITEM_EFFECT://肤质
                view = LayoutInflater.from(context).inflate(R.layout.classfiy_item_effect, viewGroup, false);
                viewHolder = new Effect(view,myClassDefaultItemClickListener);
                break;
        }


        return viewHolder;
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

        int itemViewType = getItemViewType(i);
        switch (itemViewType) {
            case ITEM_PROPERTY:

                break;
            case ITEM_SKIN://功效

                break;
            case ITEM_EFFECT://肤质
                Effect holdert = (Effect) viewHolder;
                List<ClassifyBean.DataBean.GoodsBriefBean> goodsBrief = data.getGoodsBrief();
                ClassifyBean.DataBean.GoodsBriefBean goodsBriefBean = goodsBrief.get(i-2);
                holdert.name.setText(goodsBriefBean.getGoods_name());
                holdert.price.setText("¥"+goodsBriefBean.getShop_price());
                holdert.price2.setText("¥"+goodsBriefBean.getMarket_price());
                holdert.price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                ImageLoader.getInstance().displayImage(goodsBriefBean.getGoods_img(),holdert.imageView, ContentData.options);
                break;
        }
    }

    @Override
    public int getItemCount() {
        return data.getGoodsBrief().size() + 2;
    }


    class Property extends RecyclerView.ViewHolder {

        public Property(View itemView) {
            super(itemView);

        }
    }

    class Skin extends RecyclerView.ViewHolder implements View.OnClickListener {
        MyClassTabLayoutClickListener myTabLayoutClickListener;
        public Skin(View itemView, MyClassTabLayoutClickListener myClassTabLayoutClickListener) {
            super(itemView);
            myTabLayoutClickListener = myClassTabLayoutClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (myTabLayoutClickListener != null) {
                myTabLayoutClickListener.onItemClick(v, getPosition());
            }
        }
    }

    class Effect extends RecyclerView.ViewHolder implements View.OnClickListener  {

        private final ImageView imageView;
        private final TextView price;
        private final TextView name;
        private final TextView price2;
        MyClassDefaultItemClickListener itemClickListener;
        public Effect(View itemView,MyClassDefaultItemClickListener itemClickListener) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.classify_item_image);
            name = (TextView) itemView.findViewById(R.id.classify_item_name);
            price = (TextView) itemView.findViewById(R.id.classify_item_price);
            price2 = (TextView) itemView.findViewById(R.id.classify_item_price2);
            this.itemClickListener = itemClickListener;
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            if (itemClickListener != null) {
                itemClickListener.onItemClick(v, getPosition());
            }
        }
    }
}
